package com.example.questionnaire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.questionnaire.dao.AllQuestionMapper;
import com.example.questionnaire.dao.EvaluateQuestionMapper;
import com.example.questionnaire.dao.QuestionnaireMapper;
import com.example.questionnaire.dao.ScoreQuestionMapper;


import com.example.questionnaire.pojo.DO.AllQuestion;
import com.example.questionnaire.pojo.DO.EvaluateQuestion;
import com.example.questionnaire.pojo.DO.Questionnaire;
import com.example.questionnaire.pojo.DO.ScoreQuestion;
import com.example.questionnaire.pojo.VO.EvaluateQuestionVO;
import com.example.questionnaire.pojo.VO.ScoreQuestionVO;
import com.example.questionnaire.service.RatingQuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RatingQuestionImpl implements RatingQuestionService {

    @Autowired
    private ScoreQuestionMapper scoreQuestionMapper;

    @Autowired
    private EvaluateQuestionMapper evaluateQuestionMapper;

    @Autowired
    private QuestionnaireMapper questionnaireMapper;

    @Autowired
    private AllQuestionMapper allQuestionMapper;
    @Override
    public boolean updateAddScoreQuestion(String formid, String subjectId, String title, String scoreSettings, boolean isMustAnswer, String questionsJumpItems, String questionsJumpArray, int questionType,String jumpingType) {
        ScoreQuestionVO scoreQuestionVO = new ScoreQuestionVO();
        scoreQuestionVO.setQuestionType(questionType);
        scoreQuestionVO.setScoreSettings(scoreSettings);
        scoreQuestionVO.setQuestionsJumpItems(questionsJumpItems);
        scoreQuestionVO.setQuestionsJumpArray(questionsJumpArray);
        scoreQuestionVO.setFormid(formid);
        scoreQuestionVO.setSubjectId(subjectId);
        scoreQuestionVO.setTitle(title);
        scoreQuestionVO.setIsMustAnswer(isMustAnswer? 1 : 0);
        scoreQuestionVO.setJumpingType(jumpingType);
        //判断是否已有数据，有就更新，没就新增
        QueryWrapper<AllQuestion> wrapperSubjectId = new QueryWrapper<>();
        wrapperSubjectId.eq("subject_id",scoreQuestionVO.getSubjectId());
        if(allQuestionMapper.selectOne(wrapperSubjectId) != null){
            UpdateWrapper<AllQuestion> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("subject_id",scoreQuestionVO.getSubjectId())
                    .set("title",scoreQuestionVO.getTitle());
            int wm = allQuestionMapper.update(null, updateWrapper);
            if (wm < 1){
                throw new RuntimeException("插入失败");
            }
            UpdateWrapper<ScoreQuestion> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.eq("subject_id",scoreQuestionVO.getSubjectId())
                            .set("title",scoreQuestionVO.getTitle())
                                    .set("score_settings",scoreQuestionVO.getScoreSettings())
                                            .set("is_must_answer",scoreQuestionVO.getIsMustAnswer())
                                                    .set("questions_jump_items",scoreQuestionVO.getQuestionsJumpItems())
                                                            .set("questions_jump_array",scoreQuestionVO.getQuestionsJumpArray())
                                                                .set("jumping_type",scoreQuestionVO.getJumpingType());
            if (scoreQuestionMapper.update(null,updateWrapper1) < 1) {
                throw new RuntimeException("插入失败");
            }
            return false;

        }
//        String formid = scoreQuestionVO.getFormid();
        //将题目数量（顺序）储存到对应的问题表和问卷表里面
        QueryWrapper<Questionnaire> wrapper = new QueryWrapper<>();
        wrapper.eq("formid", formid);
        formid = formid.trim();
        Questionnaire questionnaire = questionnaireMapper.selectOne(wrapper);
        int questionNumber = questionnaire.getQuestionNumber();
        questionNumber ++;
        UpdateWrapper<Questionnaire> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("formid", formid)
                .set("question_number", questionNumber);
        if (questionnaireMapper.update(null, updateWrapper) < 1) {
            throw new RuntimeException("插入失败");
        }

        //将数据存进所有问题表里
        AllQuestion allQuestion = new AllQuestion();
        allQuestion.setFormid(formid);
        allQuestion.setQuestionType(questionType);
        allQuestion.setSubjectId(scoreQuestionVO.getSubjectId());
        allQuestion.setTitle(scoreQuestionVO.getTitle());
        allQuestion.setSubjectNum(questionNumber);

        int i = allQuestionMapper.insert(allQuestion);
        if (i < 1) {
            throw new RuntimeException("插入失败");
        }
        ScoreQuestion scoreQuestion = new ScoreQuestion();
        BeanUtils.copyProperties(scoreQuestionVO, scoreQuestion);
        //将数据存进评分表
        int k = scoreQuestionMapper.insert(scoreQuestion);
        if (k < 1) {
            throw new RuntimeException("插入失败");
        }
        return true;
    }

    @Override
    public boolean updateAddEvaluateQuestion(String formid, String subjectId, String title, String evaluation, boolean isMustAnswer, String questionsJumpItems, String questionsJumpArray, int questionType,String jumpingType){
        EvaluateQuestionVO evaluateQuestionVO = new EvaluateQuestionVO();
        evaluateQuestionVO.setQuestionType(questionType);
        evaluateQuestionVO.setEvaluation(evaluation);
        evaluateQuestionVO.setQuestionsJumpItems(questionsJumpItems);
        evaluateQuestionVO.setQuestionsJumpArray(questionsJumpArray);
        evaluateQuestionVO.setFormid(formid);
        evaluateQuestionVO.setSubjectId(subjectId);
        evaluateQuestionVO.setTitle(title);
        evaluateQuestionVO.setIsMustAnswer(isMustAnswer ? 1 : 0);
        evaluateQuestionVO.setJumpingType(jumpingType);
        //判断是否已有数据，有就更新，没就新增
        QueryWrapper<AllQuestion> wrapperSubjectId = new QueryWrapper<>();
        wrapperSubjectId.eq("subject_id",evaluateQuestionVO.getSubjectId());
        if(allQuestionMapper.selectOne(wrapperSubjectId) != null){
            UpdateWrapper<AllQuestion> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("subject_id",evaluateQuestionVO.getSubjectId())
                    .set("title",evaluateQuestionVO.getTitle());
            int wm = allQuestionMapper.update(null, updateWrapper);
            if (wm < 1){
                throw new RuntimeException("插入失败");
            }
            UpdateWrapper<EvaluateQuestion> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.eq("subject_id",evaluateQuestionVO.getSubjectId())
                    .set("title",evaluateQuestionVO.getTitle())
                    .set("evaluation",evaluateQuestionVO.getEvaluation())
                    .set("is_must_answer",evaluateQuestionVO.getIsMustAnswer())
                    .set("questions_jump_items",evaluateQuestionVO.getQuestionsJumpItems())
                    .set("questions_jump_array",evaluateQuestionVO.getQuestionsJumpArray())
                    .set("jumping_type",evaluateQuestionVO.getJumpingType());
            if (evaluateQuestionMapper.update(null,updateWrapper1) < 1) {
                throw new RuntimeException("插入失败");
            }
            return false;

        }
//        String formid = evaluateQuestionVO.getFormid();
        //将题目数量（顺序）储存到对应的问题表和问卷表里面
        QueryWrapper<Questionnaire> wrapper = new QueryWrapper<>();
        wrapper.eq("formid", formid);
        formid = formid.trim();
        Questionnaire questionnaire = questionnaireMapper.selectOne(wrapper);
        int questionNumber = questionnaire.getQuestionNumber();
        questionNumber ++;
        UpdateWrapper<Questionnaire> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("formid", formid)
                .set("question_number", questionNumber);
        if (questionnaireMapper.update(null, updateWrapper) < 1) {
            throw new RuntimeException("插入失败");
        }

        //将数据存进所有问题表里
        AllQuestion allQuestion = new AllQuestion();
        allQuestion.setFormid(formid);
        allQuestion.setQuestionType(questionType);
        allQuestion.setSubjectId(evaluateQuestionVO.getSubjectId());
        allQuestion.setTitle(evaluateQuestionVO.getTitle());
        allQuestion.setSubjectNum(questionNumber);

        int i = allQuestionMapper.insert(allQuestion);
        if (i < 1) {
            throw new RuntimeException("插入失败");
        }
        EvaluateQuestion evaluateQuestion = new EvaluateQuestion();
        BeanUtils.copyProperties(evaluateQuestionVO, evaluateQuestion);
        //将数据存进评分表
        int k = evaluateQuestionMapper.insert(evaluateQuestion);
        if (k < 1) {
            throw new RuntimeException("插入失败");
        }
        return true;
    }

}
